package com.crowdsourcing.controller;

import com.crowdsourcing.utils.TokenHS256;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Map;

import static com.crowdsourcing.utils.TokenHS256.ValidToken;

/**
 * @author renbaojia 1406423298@qq.ocom
 * @version 1.0
 * @CREATED 2019/3/5 10:49:40
 * @des
 * @修改记录:
 **/

public class MyHandlerInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        httpServletRequest.getRequestURI();
        final String token = httpServletRequest.getHeader("token");
        final Map<String, Integer> map = ValidToken(token);
        Integer status = map.get("status");
        if (status == 0) {
            System.out.println("签名校验成功" + "用户id：" + map.get("uid"));
            return false;
        } else if (status == 1) {
            System.out.println("token过期");
            return true;
        } else {
            System.out.println("非法签名");
            return true;
        }


    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {

    }
}
